package de.szut.traineeslife;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.MenuInflater;
import android.widget.ArrayAdapter;
import android.widget.FrameLayout;

import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.app.ActionBar.OnNavigationListener;
import com.actionbarsherlock.app.SherlockFragmentActivity;
import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuItem;

import de.szut.traineeslife.adapter.PagerAdapter;
import de.szut.traineeslife.db.DataBaseHandler;
import de.szut.traineeslife.fragments.DayReportFragment;
import de.szut.traineeslife.fragments.ReportFragment;
import de.szut.traineeslife.utils.Constants;
import de.szut.traineeslife.utils.DateHelper;
import de.szut.traineeslife.utils.ProfilHelper;
import de.szut.traineeslife.utils.ReportsHelper;
import de.szut.traineeslife.utils.ReportsListener;

public class TraineesLifeActivity extends SherlockFragmentActivity implements ReportsListener{

	private ActionBar mActionBar;
	private ViewPager mViewPager;
	private String[] mNavigationItems;
	private String[] mPagerContent;
	private DateHelper mDateHelper;
	private ProfilHelper mProfilHelper;
	private ReportsHelper mReportsHelper;
	private String extraAcc;
	private DataBaseHandler dbh;

	/* 
	 * Is The Mainpart og this Application, in this Activity starts a Viewpager to create each Report
	 * (non-Javadoc)
	 * @see android.support.v4.app.FragmentActivity#onCreate(android.os.Bundle)
	 */
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.traineeslife_activity);
		mViewPager = (ViewPager) findViewById(R.id.pager);
		if (getIntent().hasExtra(Constants.PREFS_TAG)) {
			Bundle extra = getIntent().getExtras();
			extraAcc = extra.getString(Constants.PREFS_TAG);
		}
		if (getIntent().hasExtra(Constants.DATEHELPER_TAG)) {
			mDateHelper = (DateHelper) getIntent().getSerializableExtra(Constants.DATEHELPER_TAG);
		}
		if(getIntent().hasExtra(Constants.PROFILHELPER_TAG)){
			mProfilHelper = (ProfilHelper) getIntent().getSerializableExtra(Constants.PROFILHELPER_TAG);
		}
		dbh = new DataBaseHandler(this);
		mReportsHelper = new ReportsHelper();
		mNavigationItems = mDateHelper.getYearsOfEducation();// liste der Jahre
		mActionBar = getSupportActionBar();
		mActionBar.setDisplayShowTitleEnabled(false);
		mActionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);
		ArrayAdapter<String> abList = new ArrayAdapter<String>(this, android.R.layout.simple_dropdown_item_1line, mNavigationItems);
		ActionBar.OnNavigationListener navigationListener = new OnNavigationListener() {

			@Override
			public boolean onNavigationItemSelected(int itemPosition, long itemId) {
				mDateHelper.setSelectedYear(mNavigationItems[itemPosition]);
				mPagerContent = mDateHelper.getWeeksOfSelectedYear();
				createViewPager(mViewPager, mPagerContent);
				return false;
			}
		};
		mActionBar.setListNavigationCallbacks(abList, navigationListener);
	}
	
	/**Creates The Viepager with the correct Content for the PagerStripes
	 * @param pager
	 * @param pagerContent
	 */
	private void createViewPager(ViewPager pager , String[] pagerContent){
		pager.setAdapter(null);
		PagerAdapter pagerAdapter = new PagerAdapter(getSupportFragmentManager(), pagerContent,addPagerFragments(pagerContent),mDateHelper,this);
		pager.setAdapter(pagerAdapter);
	}
	
	/**Adds The Fragments to the Pager for eacht pagertitel
	 * 
	 * @param pagerContent
	 * @return
	 */
	private Fragment[] addPagerFragments(String[] pagerContent){
		Fragment[] fragments = new Fragment[pagerContent.length];
		for(int i=0;i<pagerContent.length;i++){
//			if(mProfilHelper.isWeeklyReport() == true){
				fragments[i] = new ReportFragment(this,mReportsHelper);
//			}else{
//				fragments[i] = new DayReportFragment();
//			}
		}
		return fragments;
		
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		com.actionbarsherlock.view.MenuInflater inflater = getSupportMenuInflater();
		inflater.inflate(R.menu.main, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case R.id.action_settings:

			break;
		case R.id.action_manage:
			Intent i = new Intent(TraineesLifeActivity.this, ReportsListActivity.class);
			startActivity(i);
			break;
		case R.id.action_save:
			
			break;

		default:
			break;
		}
		return super.onOptionsItemSelected(item);
	}

	@Override
	public void getReportsData(String text,String hours) {
		mReportsHelper.setReportsText(text);
		mReportsHelper.setReportsHours(hours);
		
	}

	@Override
	public void setReportStartDate(String startDate) {
		mReportsHelper.setReportStart(startDate);
		
	}

	@Override
	public void setReportEndDate(String endDate) {
		mReportsHelper.setReportEnd(endDate);
		
	}

	@Override
	public void setReportTitel(String titel) {
		mReportsHelper.setReportTitel(titel);
		
	}
}
